package com.kly.user.task;

import cn.hutool.core.net.NetUtil;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.google.common.collect.Maps;
import com.kly.dto.ConsumptionUserDto;
import com.kly.service.UserVasPurchasesService;
import com.kly.user.db.dao.ChatterLevelDao;
import com.kly.user.db.dao.UserDao;
import com.kly.user.db.dao.UserMsgSendTotalDao;
import com.kly.user.db.entity.ChatterLevelEntity;
import com.kly.user.db.entity.UserMsgSendTotalEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Component
public class ChatterLevelTask {

    private static Log logger = LogFactory.getLog(ChatterLevelTask.class);

    @Resource
    private UserDao userDao;

    @Resource
    private UserMsgSendTotalDao userMsgSendTotalDao;

    @Resource
    private UserVasPurchasesService userVasPurchasesService;

    @Resource
    private ChatterLevelDao chatterLevelDao;

    @NacosValue(value = "${task.job.executor.ip}", autoRefreshed = true)
    private String taskJobExecutor;

    private String localIP = NetUtil.getLocalhostStr();


    @Scheduled(fixedDelay = 150000)
    public void execute() {

        if (!taskJobExecutor.contains(localIP)) {
            logger.info("=============ChatterLevelTask, currentLocalAddress:{}, onlyExecuteTaskIn:{}", localIP, taskJobExecutor);
            return;
        }

        //查询所有的兼职chatter
        List<String> chatterCodeList = userDao.getChatterList(2);
        for (String chatterCode : chatterCodeList) {

            if (chatterCode.equals("294341891275620566")) {
                int a = 1;
            }
            int chatterCount = 0;
            List<ConsumptionUserDto> consumptionUserDtoList = userVasPurchasesService.unLockList(chatterCode);
            for (ConsumptionUserDto consumptionUserDto : consumptionUserDtoList) {
                UserMsgSendTotalEntity userMsgSendTotalEntity = userMsgSendTotalDao.getUserSendMsgNum(chatterCode, consumptionUserDto.getUserCode(), consumptionUserDto.getChatUserCode());
                if (userMsgSendTotalEntity != null && userMsgSendTotalEntity.getMsgNum() != null && userMsgSendTotalEntity.getMsgNum() > 30) {
                    chatterCount = chatterCount + 1;
                }
            }
            int chatterLevel = getChatterLevel(chatterCount);

            final HashMap<String, Object> params = Maps.newHashMap();
            params.put("chatterCode", chatterCode);
            List<ChatterLevelEntity> chatterLevelEntityList = chatterLevelDao.selectByParams(params);
            ChatterLevelEntity chatterLevelEntity = new ChatterLevelEntity();
            chatterLevelEntity.setChatterCode(chatterCode);
            chatterLevelEntity.setLevel(chatterLevel);
            if (chatterLevelEntityList.isEmpty()) {
                chatterLevelDao.insert(chatterLevelEntity);
            } else {
                chatterLevelEntity.setId(chatterLevelEntityList.get(0).getId());
                chatterLevelDao.updateByPrimaryKey(chatterLevelEntity);
            }
        }
        //先判断是否卖出去ppv 再判断会话数
        //根据总的数量判断等级并保存等级
    }

    /**
     * 读取主播等级 1尾部主播 2腰部主播 3头部主播
     *
     * @param count
     * @return
     */
    private int getChatterLevel(int count) {
        if (count < 31) {
            return 3;
        } else if (count < 81) {
            return 2;
        } else {
            return 1;
        }
    }
}
